<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item label="旧密码" prop="oldPasswd">
      <el-input v-model.trim.trim="user.oldPasswd" placeholder="请输入旧密码" type="password" />
    </el-form-item>
    <el-form-item label="新密码" prop="newPasswd">
      <el-input v-model.trim="user.newPasswd" placeholder="请输入新密码" type="password" />
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPasswd">
      <el-input v-model.trim="user.confirmPasswd" placeholder="请确认密码" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from "@/api/system/user";
import { getKeys } from "@/api/login";
const sm2 = require('sm-crypto').sm2
const cipherMode = 1

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPasswd !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      // 公钥
      pubkeys: undefined,
      test: "1test",
      user: {
        oldPasswd: undefined,
        newPasswd: undefined,
        confirmPasswd: undefined
      },
      // 表单校验
      rules: {
        oldPasswd: [
          { required: true, message: "旧密码不能为空", trigger: "blur" },
        ],
        newPasswd: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          {
            pattern: /^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{8,}$/,
            message: "密码必须包含8位或8位以上字符、大小写字母、数字、特殊符号(空格除外)",
            trigger: "blur"
          }
        ],
        confirmPasswd: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    /** 获取公钥 */
    getKeys(){
      getKeys().then((res)=>{
        if(res.status == 200){
          this.pubkeys = res.data;
        }
      });
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          getKeys().then((res)=>{
            if(res.status == 200){
              this.pubkeys = res.data;
              let param = {
                parameter: {
                  oldPasswd: "04"+sm2.doEncrypt(this.user.oldPasswd, this.pubkeys, cipherMode),
                  newPasswd: "04"+sm2.doEncrypt(this.user.newPasswd, this.pubkeys, cipherMode),
                  confirmPasswd: "04"+sm2.doEncrypt(this.user.confirmPasswd, this.pubkeys, cipherMode),
                }
              }
              updateUserPwd(param).then(
                response => {
                  if(response.status == 200){
                    this.msgSuccess("修改成功");
                  }else if(response.status == 101){
                    this.msgError(response.errors[0].message);
                  }
                }
              );
            }
          });
        }
      });
    },
    close() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/index" });
    }
  }
};
</script>
